package org.tissou.client.Panel_Premier.comptes_tab.ModificationsVisualisation_Comptes;

import org.tissou.client.Interface.Base_Panel;
import org.tissou.client.Services.*;
import org.tissou.client.widgets.Label_MessageInformation;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 27-Apr-2009
 * Time: 22:14:53
 * To change this template use File | Settings | File Templates.
 */
public class Fenetre_Ajout_Compte extends Base_Panel {
  DockPanel principal = new DockPanel();

  ArbreDeComptes comptes = new ArbreDeComptes(InformationsGlobales.personneLoguee.getNom());
  Label label_nom = new Label("Quel est le nom du compte?");
  TextBox entreenom = new TextBox();
  Label label_comptes = new Label("Voici vos comptes deja existants");
  Label label_monnaie = new Label("Choisis la monnaie de ton nouveau compte");
  ArbreDeMonnaies arbremonnaies = new ArbreDeMonnaies();

  DockPanel panel_monnaies = new DockPanel();
  DockPanel panel_comptes = new DockPanel();
  DockPanel panel_nom = new DockPanel();

  Button ok = new Button("Je veux un nouveau compte !!!");

    Label_MessageInformation message_information = new Label_MessageInformation();


  protected void MiseEnPlaceStyles() {
    panel_monnaies.addStyleName("dockPanelMonnaies");
    panel_comptes.addStyleName("dockPanelCategories");
   

  }

  protected void MiseEnPlaceParametresWidgets() {
    

  }

  protected void MiseEnPlaceParametresPanel() {
    panel_monnaies.add(label_monnaie, DockPanel.NORTH);
    panel_monnaies.add(arbremonnaies, DockPanel.NORTH);
    panel_comptes.add(label_comptes, DockPanel.NORTH);
    panel_comptes.add(comptes, DockPanel.NORTH);
    panel_nom.add(label_nom, DockPanel.NORTH);
    panel_nom.add(entreenom, DockPanel.WEST);
    panel_nom.add(ok, DockPanel.WEST);

  }

  protected void MiseEnPlaceActions() {
    ok.addClickHandler(new ClickHandler(){

        @Override
        public void onClick(ClickEvent event) {
            if (entreenom.getText().equals("")) {
                message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE,"Aucun nom n'a ete rentre");
           }
           else {
               final AsyncCallback c = new AsyncCallback(){
                    public void onFailure(Throwable caught) {
                        System.out.println("Creer Compte: Ca a pas marche pas de chance");
                         message_information.setMessage(Label_MessageInformation.Etat.SERVICEPASMARCHE,caught.getLocalizedMessage());
                     }

                     public void onSuccess(Object result) {
                     System.out.println("Creer Compte: Ca a marche ! You are the best of the world !");
                      message_information.setMessage(Label_MessageInformation.Etat.SERVICEMARCHE,"Le compte a bien ete cree !");
                   }
                };
                message_information.setMessage(Label_MessageInformation.Etat.SERVICEATTENTE, "En attente d'une confirmation du serveur");

                MonnaieInfos monnaie_selectionnee;
                try {monnaie_selectionnee = arbremonnaies.donneLaMonnaieSelectionnee();
                } catch (MonException e){
                  message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE, e.getMessage());
                  throw e;
                }

                NouveauCompteService.App.getInstance().CreerNouveauCompte(entreenom.getText(),
                        InformationsGlobales.personneLoguee,monnaie_selectionnee, c );
           }
        }
    });
  }

  protected void MiseEnPlacePanels() {
    principal.add(message_information, DockPanel.NORTH);
    principal.setCellHeight(message_information, "40px");
    principal.setCellHorizontalAlignment(message_information,HasHorizontalAlignment.ALIGN_CENTER);
    principal.add(panel_comptes, DockPanel.WEST);
    principal.add(panel_monnaies, DockPanel.WEST);
    principal.add(panel_nom, DockPanel.WEST);
  }

  protected Panel donneLePanelPrincipal() {
    return principal;
  }
}
